<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">

      <el-form-item label="患者姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入患者姓名" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idcardNum">
        <el-input v-model="queryParams.idcardNum" placeholder="请输入身份证号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">


      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="userName" />
      <el-table-column label="患者头像" width="100" align="center" prop="userAvatar">
        <template #default="scope">
          <img width="50" height="50" :src="scope.row.userAvatar" alt="">
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idcardNum" />
      <el-table-column label="手机号" align="center" prop="phone" />


      <el-table-column label="检查详情" align="center" prop="medicalRecordDetail">
        <template #default="scope">
          <el-button link type="primary" icon="ZoomIn" @click="handleDetail(scope.row)">检查详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />




    <el-dialog title="详情检查" v-model="open" width="1080px" append-to-body>
      <template #header>
        <div>
          <!--  -->
          <Header title="检查档案详情" :queryParams="form" />
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">

            <el-form-item label="就诊日期" prop="date">
              <el-date-picker v-model="queryParams.date" style="width: 150px" format="YYYY/MM/DD"
                value-format="YYYY-MM-DD" type="date" placeholder="请选择日期" />
            </el-form-item>
            <el-form-item label="就诊部门" prop="departmentName">
              <el-input v-model="queryParams.departmentName" placeholder="请输入就诊部门" clearable style="width: 150px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="就诊医生" prop="employeeName">
              <el-input v-model="queryParams.employeeName" placeholder="请输入就诊医生" clearable style="width: 150px"
                @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <div class="flex">
        <div style="flex: 1 1 50%; flex-grow: 1;">
          <el-table height="450px" scrollbar-always-on highlight-current-row size="small" :data="tableData"
            @current-change="handleCurrentChange">
            <el-table-column label="序号" align="center" type="index" width="55">
              <template #default="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <!-- <el-table-column prop="medicalRecordId" label="检查编号" /> -->
            <el-table-column prop="createTime" label="就诊时间" />

            <el-table-column prop="departmentName" label="就诊部门" />
            <el-table-column prop="employeeName" label="就诊医生" />
          </el-table>
        </div>

        <div class="detail" style="flex: 1 1 50%;">
          <el-form size="medium" class="p-[16px]" disabled ref="noticeRef" :model="form" :rules="rules">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="就诊部门">
                  <el-input v-model="form.departmentName" placeholder="" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="就诊日期">
                  <el-input v-model="form.createTime" placeholder="" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-divider style="margin: 0px 0px 10px 0px;;padding:0px;" />
            <div class="flex justify-center align-center">
              <h2 style="margin: 5px 0px;">
                就诊检查
              </h2>
            </div>
            <el-divider style="margin: 10px 0px;;padding:0px;" />


            <el-row :gutter="10" v-for="item in form.itemList" :key="item.id">
              <el-col :span="24">
                <el-form-item label="项目名称" label-width="80px" label-position="left">
                  <el-input v-model="item.name" placeholder="" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" label-width="80px" label-position="left">
                  <el-input v-model="item.remark" placeholder="" />
                </el-form-item>
              </el-col>
              <el-divider style="margin: 10px 0px;;padding:0px;" />
            </el-row>





            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="就诊医生">
                  <el-input v-model="form.employeeName" placeholder="" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="业务审核">
                  <el-input v-model="form.userHeight" placeholder="" />
                </el-form-item>
              </el-col>
            </el-row>


          </el-form>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script setup name="Notice">

import Header from '../common/header.vue'


const tableData = ref([])
import {
  medicalApiCheckDetail,
  medicalApiCheckDetailList,
  listCheck,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/backMedical/backMedical";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type, sys_user_sex } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type",
  "sys_user_sex"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);

const openDetail = ref(false)
// 禁用所有表单
const formDisable = ref(false)
const gridData = ref([])


/**详情按钮操作 */
function handleDetail(row) {
  // openDetail.value = !openDetail.value
  reset();
  const appUserId = row.appUserId || ids.value;
  queryParams.value.appUserId = appUserId
  medicalApiCheckDetailList(queryParams.value).then((response) => {
    tableData.value = response;
    open.value = true;
    title.value = "详情检查";
  });
  formDisable.value = true
}

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listCheck(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();



  // 获取appUserId
  medicalApiCheckDetailList(queryParams.value).then((response) => {
    tableData.value = response;
    open.value = true;
    title.value = "详情处方";
  });
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公告";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const noticeId = row.noticeId || ids.value;
  getNotice(noticeId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改公告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}


function handleCurrentChange(row) {
  if (row?.medicalCheckId) {
    medicalApiCheckDetail(row.medicalCheckId).then((response) => {
      form.value = response;
    })
  }
}

getList();
</script>


<style lang="scss" scoped>
.detail {
  :deep(.el-input__wrapper.is-focus) {
    box-shadow: none !important;
  }

  :deep(.el-input__wrapper:hover) {
    box-shadow: none !important;
  }

  :deep(.el-input__wrapper) {
    box-shadow: none !important;
  }

  :deep(.el-input.is-disabled .el-input__wrapper) {
    background-color: #ffff !important;
  }

  :deep(.el-input.is-disabled .el-input__inner) {
    color: #000 !important;
    -webkit-text-fill-color: #000 !important;
  }

  :deep(.el-textarea__inner) {
    background-color: #ffff !important;
    border: none !important;
    box-shadow: none !important;
    color: #000 !important;
    -webkit-text-fill-color: #000 !important;
  }
}
</style>
